﻿using System;
using System.Linq.Expressions;
using Samurai.Wakizashi.Validation;
using Samurai.Wakizashi.Workflow;

namespace Reactor.Workflow
{
    public class ContextValidationStep<T> : WorkflowStepBase<T> where T : class, IWorkflowContext 
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ContextValidationStep&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        public ContextValidationStep(IWorkflow<T> workflow)
        {
            Workflow = workflow;
        }

        public IRuleBinding<T, TProperty> For<TProperty>(Expression<Func<T, TProperty>> expression)
        {
            return Workflow.ContextValidator.For(expression);
        }

        #region Overrides of WorkflowStepBase<T>

        public override void ExecuteStep()
        {
            var result = Workflow.ContextValidator.Validate();

            if(!result.IsValid)
                NotifyStepFailed(new WorkflowContextValidationException(result.GetFirstMessage()));
            else
                NotifyStepCompleted();
        }

        #endregion
    }
}
